package com.syzg.webservice.springService;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import Decoder.BASE64Decoder;

import com.syzg.base.module.service.ModuleService;
import com.syzg.common.util.FileUtil;
import com.syzg.common.util.PKCreator;
import com.syzg.common.util.SpringUtil;
import com.syzg.core.util.DateUtil;
import com.zenithsun.news.news.model.NewsDetailInfo;
import com.zenithsun.news.news.model.NewsInfo;
import com.zenithsun.news.news.service.NewsService;
import com.zenithsun.news.newscolumns.model.NewsColumns;
import com.zenithsun.news.newsinfo.service.NewsinfoService;

public class WebServiceI {

	public String getModule() {
		try {
			ModuleService moduleService = (ModuleService) SpringUtil
					.getBean("moduleService");
			return moduleService.getModuleNameByModuleCode("in_out");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean addNews(String json) {

		boolean flag = true;
		try {
			NewsService services = (NewsService) SpringUtil
					.getBean("newsService");
			NewsinfoService newsinfoService = (NewsinfoService) SpringUtil
					.getBean("newsinfoService");
			// 解析 JSON
			JSONObject news = new JSONObject(json);
			// 转换为 model
			NewsInfo ni = new NewsInfo();

			String pk = PKCreator.getPk();
			String newsColumnId = news.getString("newsColumnsId");
			// NewsColumns
			NewsColumns columns = new NewsColumns();
			columns.setId(newsColumnId);
			// PK
			ni.setId(pk);
			ni.setNewsColumns(columns);
			ni.setTitle(news.getString("title"));
			ni.setContent(news.getString("content"));
			ni.setCorrespondent(news.getString("correspondent"));
			ni.setCoverPath(news.getString("coverPath"));
			ni.setCreateTime(new Date());
			ni.setEditor(news.getString("editor"));
			ni.setIsCarousel(Short.parseShort(news.getString("isCarousel")));
			ni.setIsStriking(Short.parseShort(news.getString("isStriking")));//
			ni.setIsTop(Short.parseShort(news.getString("isTop")));
			ni.setKeyword(news.getString("keyword"));
			ni.setPhotographer(news.getString("photographer"));
			ni.setNewstype(news.getString("newstype"));
			services.create(ni);
			// 获取 pictures
			JSONArray pictures = news.getJSONArray("pictures");
			NewsDetailInfo detail = null;
			for (int i = 0; i < pictures.length(); i++) {
				JSONObject o = pictures.getJSONObject(i);
				// 二进制转换图片
				detail = new NewsDetailInfo();
				detail.setPath(writeFile(o.getString("picData"),
						o.getString("type")));
				detail.setNewsInfo(ni);
				detail.setOrderNo(i * 10);
				detail.setShootingTime(new Date());
				detail.setId(PKCreator.getPk());
				newsinfoService.create(detail);
			}
			// pictures.

		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}

	public static String writeFile(String str, String type) {
		String imageFile = "";
		try {
			String path = WebServiceI.class.getClassLoader().getResource("")
					.getPath();
			path = path.substring(0, path.indexOf("WEB-INF"))
					+ "zenithsun/pic/images";
			String subDir = DateUtil.format(new Date(), "yyyy/MMdd");
			String upPath = path + File.separator + subDir;
			FileUtil.createDirCur(upPath);
			imageFile = upPath + new Date().getTime() + "." + type;
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] bytes = decoder.decodeBuffer(str);
			File file = new File(imageFile);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bytes);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String separator = "/images";
		imageFile = imageFile.substring(imageFile.indexOf(separator)
				+ separator.length());
		imageFile = imageFile.replaceAll("\\\\", "/");
		return imageFile;
	}
}
